<?php

class model_order extends framework_base_model
{

    private static $table = 'weixin_order';

    /**
     * 查询订单情况
     * @author guohui
     */
    public static function  getOrderList($sales_customer, $agreement_no, $openId)
    {
        $sql = 'select agreement_no,order_id,sales_customer  from ' . self::$table . ' where delete_flag = 0  ';
        if (!empty($sales_customer)) {
            $sql .="and sales_customer like '%".$sales_customer."%'";
        }
        if (!empty($agreement_no)) {
            $sql .=" and agreement_no = '".$agreement_no."'";
        }
        if (!empty($openId)) {
            $sql .=" and sales_id like '%".$openId."%'";
        }
        $sql .= ' group by agreement_no order by raw_update_time desc';
        //framework_static_function::write_log ( __METHOD__ . ":" .$sql, 'weixin_sql' );
        try {
            $st = self::DB()->dbh->prepare($sql);
            $st->execute();
            $list = $st->fetchAll();
            return $list;
        } catch ( Exception $e ) {
            framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
        }
    }

    /**
     *
     * 查询某订单合同号
     */
    public static function getAgreementNo($orderId,$openId){
        $sql = 'select agreement_no from ' . self::$table . ' where delete_flag = 0  ';

        if (!empty($orderId)) {
            $sql .=" and order_id = ".intval($orderId);
        }
        if (!empty($openId)) {
            $sql .=" and sales_id like '%".$openId."%'";
        }
        $sql .= ' order by raw_update_time desc';
        //framework_static_function::write_log ( __METHOD__ . ":" .$sql, 'weixin_sql' );
        try {
            $st = self::DB()->dbh->prepare($sql);
            $st->execute();
            $info = $st->fetch();
            return $info['agreement_no'];
        } catch ( Exception $e ) {
            framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
        }
    }

    /**
     * 查询该合同号 所有订单
     *
     */
    public static function  getOrderListForAgreementNo($agreement_no, $openId)
    {
        //$sql = 'select o.*,p.produce_name from ' . self::$table . ' as o ,weixin_product as p where o.delete_flag = 0  ';
        $sql = 'select * from ' . self::$table . ' as o  where o.delete_flag = 0  ';
        if (!empty($agreement_no)) {
            $sql .=" and o.agreement_no = '".$agreement_no."'";
        }
        if (!empty($openId)) {
            $sql .=" and o.sales_id like '%".$openId."%'";
        }
        //$sql .= ' and o.produce_id = p.product_id order by o.raw_update_time desc limit 30';
        $sql .= ' order by o.raw_update_time desc limit 30';
        //framework_static_function::write_log ( __METHOD__ . ":" .$sql, 'weixin_sql' );
        try {
            $st = self::DB()->dbh->prepare($sql);
            $st->execute();
            $list = $st->fetchAll();
            return $list;
        } catch ( Exception $e ) {
            framework_static_function::write_log ( __METHOD__ . ":" . $e->getMessage (), 'weixin_sql' );
        }
    }
}
